import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Headers, RequestOptions} from '@angular/http';
import {Observable}     from 'rxjs/Observable';
import {Article} from "../entity/article";
import {AuthService} from "./auth.service";
import {Comment} from "../entity/comment";

@Injectable()
export class ArticleService {

  private httpUrl = 'http://localhost:8080/article';

  constructor(private http: Http, public authService: AuthService) {
  }

  private extractData(res: Response) {
    const body = res.json();
    return body || {};
  }

  private handleError(error: any) {
    // In a real world app, we might use a remote logging infrastructure
    // We'd also dig deeper into the error to get a better message
    const errMsg = (error.message) ? error.message :
      error.status ? `${error.status} - ${error.statusText}` : 'Server error';
    console.error(errMsg); // log to console instead
    return Observable.throw(errMsg);
  }

  getArticlesByUser(page:number): Observable<Article[]> {
    const headers = new Headers({
      'Content-Type': 'application/json',
      'authorization': this.authService.getCompactJws()});
    const options = new RequestOptions({headers: headers});

    return this.http.post(`${this.httpUrl}/secure/all`, page, options)
      .map(this.extractData)
      .catch(this.handleError);
  }

  getArticles(page:number): Observable<Article[]> {
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});

    return this.http.get(`${this.httpUrl}/all?page=${page}`, options)
      .map(this.extractData)
      .catch(this.handleError);
  }

  insertArticle(article: Article): void {
    const headers = new Headers({
      'Content-Type': 'application/json',
      'authorization': this.authService.getCompactJws()
    });
    const options = new RequestOptions({headers: headers});
    this.http.post(`${this.httpUrl}/secure/insert`, article, options).toPromise()
      .then((res) => console.log(this.extractData(res)))
      .catch(this.handleError);
  }

  delete(articleId: string): Observable<number> {
    const headers = new Headers({
      'Content-Type': 'application/json',
      'authorization': this.authService.getCompactJws()
    });
    const options = new RequestOptions({headers: headers});
    return this.http.post(`${this.httpUrl}/secure/delete`, articleId, options)
      .map(this.extractData)
      .catch(this.handleError);
  }

  insertComment(comment: Comment): Observable<number> {
    const headers = new Headers({
      'Content-Type': 'application/json',
      'authorization': this.authService.getCompactJws()
    });
    const options = new RequestOptions({headers: headers});
    return this.http.post(`${this.httpUrl}/secure/comment`, comment, options)
      .map(this.extractData)
      .catch(this.handleError);
  }

   getArticle(id:string):Observable<Article> {
   const headers = new Headers({ 'Content-Type': 'application/json' });
   const options = new RequestOptions({ headers: headers });

   return this.http.get(`${this.httpUrl}/${id}`, options)
     .map(this.extractData)
     .catch(this.handleError);
   }
}
